﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QlrrPrintService.MessageForm;
using System.Drawing;
using System.Windows.Forms;
using QlrrPrintService.Common;
using PluginBase;

namespace QlrrPrintService.Print
{
    public class PrintManager:IDisposable
    {
        #region 变量

        protected Dictionary<PrinterType,IPrinter> _printerDictory=new Dictionary<PrinterType,IPrinter>();

        protected PrinterState[] _errroState = 
        {
            PrinterState.OutOfPaper,
            PrinterState.OutOfPaperAndPrintHeadOpen,
            PrinterState.OutOfRibAndOutOfPaperAndPrintHeadOpen,
            PrinterState.OutOfRibbon,
            PrinterState.OutOfRibbonAndOutOfPaper,
            PrinterState.OutOfRibbonAndPaperboard,
            PrinterState.OutOfRibbonAndPaperboardAndPrintHeadOpen,
            //PrinterState.Paperboard,
            PrinterState.Pause,
            PrinterState.PrintHeadOpen,
            PrinterState.PrintHeadOpenAndOutOfRibbon,
            PrinterState.PrintHeadOpenAndPaperboard,
            PrinterState.UnKnown
        };

        #endregion

        #region 公共变量

        public static PrintManager Instance = new PrintManager(); 

        #endregion

        #region 构造函数

        private PrintManager() { }

        #endregion

        #region 公共事件

        public event OnCommandExquteCompleteHander OnExquteComplete;

        #endregion

        #region 公共函数

        public void Init(PrinterType printerType)
        {
            if (!_printerDictory.ContainsKey(printerType))
            {
                IPrinter printer = null;
                switch (printerType)
                {
                    case PrinterType.TSC:
                        printer = new TSCPrinter();
                        printer.OnCommandExcuteComplete += new OnCommandExquteCompleteHander(OnPrintCompleteHander);
                        break;
                    default:
                        printer = null;
                        break;
                }
                if (printer != null)
                {
                    if (!_printerDictory.ContainsKey(printerType))
                    {
                        _printerDictory.Add(printerType, printer);
                    }

                }
            }
            
                var state = QueryState(new PluginRequestMessage() { 
                    PrinterType=printerType,
                    PluginMessageType=PluginMessageType.QueryState
                });
                var enumState=(PrinterState)state;
                if (_errroState.Contains(enumState))
                {
                    Reset(new PluginRequestMessage() { 
                        PrinterType=printerType,
                        PluginMessageType=PluginMessageType.Reset
                    });
                }
                
            
           
        }

        public void Print(PluginRequestMessage pluginMessage)
        {
            if (pluginMessage != null)
            {
                if (_printerDictory.ContainsKey(pluginMessage.PrinterType))
                {
                    var printer = _printerDictory[pluginMessage.PrinterType];
                    if (printer != null)
                    {
                        printer.Print(pluginMessage);
                    }
                }
                
                
            }
            
        }

        public int QueryState(PluginRequestMessage pluginMessage)
        {
            if (pluginMessage != null)
            {
                if(_printerDictory.ContainsKey(pluginMessage.PrinterType))
                {
                    var printer = _printerDictory[pluginMessage.PrinterType];
                    if (printer != null)
                    {
                        return printer.QueryState(pluginMessage);
                    }
                }
               
            }
            return 0x90;
        }

        public void Reset(PluginRequestMessage pluginMessage)
        {
            if (_printerDictory.ContainsKey(pluginMessage.PrinterType))
            {
                var printer = _printerDictory[pluginMessage.PrinterType];
                if (printer != null)
                {
                    printer.Restet(pluginMessage);
                }
            }
            
        }

        public void SendCommand(PluginRequestMessage pluginMessage)
        {
            if (_printerDictory.ContainsKey(pluginMessage.PrinterType))
            {
                var printer = _printerDictory[pluginMessage.PrinterType];
                if (printer != null)
                {
                    printer.SendCommand(pluginMessage);
                }
            }
        }

        #endregion

        #region 虚函数

        protected virtual void OnPrintCompleteHander(object sender,PluginMessageType messageType, string soucreId,string msg)
        {
            //ShowMessageTip(args);
            if (OnExquteComplete != null)
            {
                OnExquteComplete(sender, messageType, soucreId, msg);
            }
        }

        protected virtual void ShowMessageTip(PrintCompleteEventArgs args)
        {
            PrintCompleteForm form = new PrintCompleteForm();
            Point p = new Point(Screen.PrimaryScreen.WorkingArea.Width - form.Width, Screen.PrimaryScreen.WorkingArea.Height);
            form.PointToScreen(p);
            form.Location = p;
            form.Show();

        }

        #endregion

        #region IDispose
        
        
        public void Dispose()
        {
            foreach (var item in _printerDictory.Values)
            {
                item.ClosePrintert();
            }
        }

        #endregion
    }
}
